var checkerFunction = {
	checkImg : function(){
		var checkResult = {
			title : "图片超标检查",
			view : "gridView",
			items : []
		};
		var images = checkerUtil.getImages();
		for(var img in images){
			var size = checkerUtil.getFileSize(img);
			var res = checkerFunction.isValidImage(img, size, images[img]);
			if(res !=0 && res <3) {//忽略轻度超标
				var errLevel;
				switch(res){
					case 1: errLevel = "error";break;
					case 2: errLevel = "warning";break;
					case 3: errLevel = "info";break;
				}
				checkResult.items.push({level:errLevel, detail:"<a href='" + img + "' target='_blank'><img src='"+img+"' width='100' height='100' title='" + (size/1024).toFixed(1) + "K' /></a>"});
			}
		}
		checker.addCheckResult(checkResult);
	},
	
	checkCSSImg : function(){
		var checkResult = {
			title : "背景图片超标检查",
			view : "gridView",
			items : []
		};
		var images = checkerUtil.getCSSImages();
		for(var img in images){
			var size = checkerUtil.getFileSize(img);
			var res = checkerFunction.isValidImage(img, size);
			if(res !=0 && res <3 ) { //忽略轻度超标
				var errLevel;
				switch(res){
					case 1: errLevel = "error";break;
					case 2: errLevel = "warning";break;
					case 3: errLevel = "info";break;
				}
				checkResult.items.push({level:errLevel, detail:"<a href='" + img + "' target='_blank'><img src='"+img+"' width='100' height='100' title='" + (size/1024).toFixed(1) + "K' /></a>"});
			}
		}
		checker.addCheckResult(checkResult);
	},
	
	/**
	 * 判断图片是否符合规范
	 * @return 结果代码.：0 符合规范；1 严重超标；2 中毒超标；3轻度超标
	 */
	isValidImage : function(url, size, dimension){
		var h,w;
		if(dimension){
			h = dimension.height;
        	w = dimension.width;
		} else {
			var img = content.document.createElement("img");
        	img.src = url;
        	 try {
	           	var req = new XMLHttpRequest();  
				req.open('GET', url, false);   
				req.send(null);  
	        } catch(exception) {
	        }
        	var h = img.height;
       		var w = img.width;
		}
        var l = h > w ? h: w;
		var f1,  f2,  f3;
		 if (l < 50) {
            f1 = 1500;
            f2 = 1.25;
            f3 = 0.5;
        }
        else if (l < 100) {
            f1 = 3200;
            f2 = 1.4;
            f3 = 1.0;
        }
        else if (l < 200) {
            f1 = 5000;
            f2 = 1.5;
            f3 = 2.0;
        }
        else if (l < 300) {
            f1 = 10000;
            f2 = 1.65;
            f3 = 4.0;
        }
        else if (l < 400) {
            f1 = 20000;
            f2 = 1.8;
            f3 = 5.0;
        }
        else if (l >= 400) {
            f1 = 10000;
            f2 = 1.8;
            f3 = 10.0;
        }
        var K = ((w * h / f1) * f2 + f3) * 1024;
        if (size / K <= 1.0) return 0; //valid
        if (size / K > 2.5 && size > 15 * 1024) {
            return 1;
        }
        else if (size / K > 1.5) {
           return 2;
        }
        else if (size / K > 1.0) {
            return 3;
        }
	},
	
	checkTracker: function() {
		var checkResult = {
			title : "监控代码检查",
			view : "listView",
			items : []
		};
        var code = content.document.body.innerHTML;
        var re = new RegExp("(http\:\/\/tk\.baidu\.com[^\"\']+)");
        var result = re.exec(code);
        if (result) {
            checkResult.items.push( { level:"info", detail:"监控代码："+result[1] } );
        }else{
        	 checkResult.items.push( { level:"error", detail:"本页面没有添加监控代码" } );
        }
        checker.addCheckResult(checkResult);
    },
    
    checkStat: function() {
		var checkResult = {
			title : "统计图片检查",
			view : "listView",
			items : []
		};
        var code = content.document.body.innerHTML;
        var re= new RegExp("http\:\/\/img\.mall\.baidu\.com\/st\/stat\.jpg([^\"\']+)");
        var result = re.exec(code);
        if (result) {
        	var str = result[1].replace("?","").replace(/&amp;/g,"|").replace(/&/g,"|");
        	var params = str.split("|");
        	if( !(/fp/.test(str)) ){
        		checkResult.items.push( { level:"warning", detail:"没有fp参数，fp表示专题的代码" } );
        	}else {
        		var re2= new RegExp("fp\=([a-zA-Z0-9_-]+)");
        		var result2 = re2.exec(result[1]); 
        		if(result2){
	        		if(!content.location.href.match(result2[1])){
	        			checkResult.items.push( { level:"error", detail:"fp参数和本专题代号不符。" } );
	        		}
        		}
        	}
        	checkResult.items.push( { level:"info", detail:"统计图片参数：" } );
        	for(var p in params){
        		checkResult.items.push( { level:"info", detail: params[p] } );
        	}
        }else{
        	 checkResult.items.push( { level:"warning", detail:"本页面没有添加统计图片" } );
        }
        checker.addCheckResult(checkResult);
    },
    
    checkSEO : function(){
    	var checkResult = {
			title : "搜索引擎优化",
			view : "listView",
			items : []
		};
		
		//check title
		checkResult.items.push( { level:"info", detail:"页面标题："+content.document.title } );
		
		//check keywords
		var keywords = checkerUtil.getSingleByXPath("//meta[@name='keywords']");
		if(keywords && keywords.content){
			checkResult.items.push( { level:"info", detail:"关键词："+ keywords.content } );
		} else {
			checkResult.items.push( { level:"warning", detail:"本页面没有设置关键词" } );
		}
		
		//check description
		var desc = checkerUtil.getSingleByXPath("//meta[@name='description']");
		if(desc && desc.content){
			checkResult.items.push( { level:"info", detail:"页面描述："+ desc.content } );
		} else {
			checkResult.items.push( { level:"warning", detail:"本页面没有设置描述信息" } );
		}
		
		//check h1
		var headings = content.document.getElementsByTagName("h1");
		if(headings && headings.length != 0){
			for( var i = 0; i < headings.length; i++ ) {
				checkResult.items.push( { level:"info", detail:"一级标题h1："+ headings[i].innerHTML } );
			}
		} else {
			checkResult.items.push( { level:"warning", detail:"本页面没有设置一级标题h1" } );
		}
		
        checker.addCheckResult(checkResult);
    },
    
    checkImgAlt : function(){
    	var checkResult = {
			title : "图片alt属性检查",
			view : "gridView",
			items : []
		};
		var images = checkerUtil.getElementsByXPath("//a/img");
		for(var i in images){
			var img = images[i];
			if(!img.alt) {
				checkResult.items.push({level:"warning", detail:"<a href='" + img.src + "' target='_blank'><img src='"+img.src+"' width='100' height='100' /></a>"});
			}
		}
		checker.addCheckResult(checkResult);
    }
};

checker.addCheckFunction(checkerFunction.checkImg);
checker.addCheckFunction(checkerFunction.checkCSSImg);
checker.addCheckFunction(checkerFunction.checkTracker);
checker.addCheckFunction(checkerFunction.checkStat);
checker.addCheckFunction(checkerFunction.checkSEO);
checker.addCheckFunction(checkerFunction.checkImgAlt);
